/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.WBUser;

import csse3004.GD.DA.WBUserDA;
import csse3004.GD.Question;
import java.lang.String;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author Lan Ye 42239015
 */
public class AnswerController extends SimpleFormController {

    private WBUserDA service = new WBUserDA();
    protected final Log logger = LogFactory.getLog(getClass());

    /**
     * Gets the a poll by pollID and return a question form this poll;
     * @param hsr Request
     * @param hsr1 Response
     * @return
     */
    @Override
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
        ModelAndView mnv = new ModelAndView("answer");
        if (hsr.getParameter("pollID") != null) {
            int next = -1;
            int questID = -1;
            Question question=null;
            int pollID = Integer.parseInt(hsr.getParameter("pollID"));
            logger.info("PollID = " + pollID);
            if (hsr.getParameter("questionID") != null) {
                questID = Integer.parseInt(hsr.getParameter("questionID"));
                if (hsr.getParameter("next") != null) {
                    if (hsr.getParameter("next").equals("true")) {
                        questID = service.getNextQuestion(pollID, true, questID);
                    } else if (hsr.getParameter("next").equals("false")) {
                        questID = service.getNextQuestion(pollID, false, questID);
                    }
                }
//                Question question = new Question().genTestQuestion(pollID).get(0);

            } else {
//            Question question = new Question().genTestQuestion(pollID).get(0);
                questID = service.getActiveQuestionID(pollID);    
            }
            
            logger.info("Active question = " + questID);
            if (questID != -1) {
                question = service.getQuestion(questID);
                mnv.addObject("question", question);
            }
            next = service.getNextQuestion(pollID, true, questID);            
            mnv.addObject("closed", (next == -1 || question == null));
            mnv.addObject("next", next);
            return mnv;
        }

        mnv.addObject("closed", true);
        return mnv;
    }
}
////          int next = WBUserService.getNextQuestion(pollID, qid);
//            int next = WBUserService.setNext(true, pollID);
//            Question question = WBUserService.getQuestion(next);
//                             
//Dummy data
//Question question = new Question().genTestQuestion(Integer.parseInt(hsr.getParameter("pollID"))).get(0);
//            int next = question.get(Integer.parseInt(hsr.getParameter("questionID"))).getId() + 1;
//             mnv.addObject("question", question);
//             mnv.addObject("closed", true);
//             if (next != -1 && !(question == null)) {
//             mnv.addObject("closed", false);
//             mnv.addObject("question", question);
//        }
//
//        }
//        else if (hsr.getParameter("pollID") != null) {
//            int pollID = Integer.parseInt(hsr.getParameter("pollID"));
//            ArrayList<Question> questions = WBUserService.getQuestions(pollID); //TODO check that it sets the question's open status
//            mnv.addObject("closed", true);
//            for (Question quest : questions) {
//                if (quest.isOpen()) {
//                    mnv.addObject("question", quest);
//                    mnv.addObject("closed", false);
//                    break;
//                }
//            }
//        }
//            mnv.addObject(question);
//            return mnv;
//
//    }
//
//    }
//}
